/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yanzhenjie.andserver.dialog;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.AttachPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.XPopupUtils;

import com.yanzhenjie.andserver.event.MessageEvent;
import com.yanzhenjie.andserver.slice.MainAbilitySlice;
import com.yanzhenjie.andserver.util.FileUtils;
import com.yanzhenjie.hos.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.app.Context;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Description: 自定义背景的Attach弹窗
 * Create by lxj, at 2019/3/13
 */
public class CustomAttachPopup2 extends AttachPopupView {
    public CustomAttachPopup2(Context context) {
        super(context, null);
    }

    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_custom_attach_popup2;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        final Text tv = (Text) findComponentById(ResourceTable.Id_tv);
        tv.setText("全部删除");
        tv.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                List<File> fileList = FileUtils.getFileList(new MainAbilitySlice().getPath());
                if (fileList.size() != 0) {
                    File file = new File(new MainAbilitySlice().getPath());
                    new XPopup.Builder(getContext())
                            .hasNavigationBar(false) // 暂未实现控制状态栏的显示隐藏
                            .hasBlurBg(true) // 暂未实现高斯模糊背景
                            .dismissOnBackPressed(true)
                            .dismissOnTouchOutside(true)
                            .hasNavigationBar(false) // 暂未实现是否显示导航栏
                            .asConfirm("", "确定要全部删除吗？",
                                    "取消", "确定",
                                    new OnConfirmListener() {
                                        @Override
                                        public void onConfirm() {
                                            try {
                                                FileUtils.deleteDir(file);
                                                EventBus.getDefault().post(new MessageEvent());
                                                FileUtils.mkDir(new MainAbilitySlice().getPath().substring(0, new MainAbilitySlice().getPath().length() - 5) + "cache");
                                            } catch (IOException e) {
                                                e.fillInStackTrace();
                                            }
                                        }
                                    }, null, false).show();


                    dismiss();
                }
                dismiss();
            }
        });
        getPopupImplView().setBackground(XPopupUtils.createDrawable(0xFFFFFFFF, popupInfo.borderRadius, 0, popupInfo.borderRadius, 0));
    }

    // 设置状态栏的高度，用以修正自定义位置弹窗的高度
    @Override
    protected int setStatusBarHeight() {
        return 130;
    }

}
